Clase Procesos

Ejercicio:

Escribir un programa que reciba a través de la línea de comandos la cantidad de procesos hijos que debe crear.

  • Cada proceso hijo debe dormir un numero aleatorio de segundos comprendido entre 0 y 30.
  • El proceso padre debe esperar la terminación de cada uno de sus hijos.
  • A medida que los hijos van terminando:
    • El padre presentará en pantalla el pid de cada uno de ellos
    • La cantidad de segundos que ha estado durmiendo cada hijo.

In [1]:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

#define NUMBER_OF_CHILDS 3
#define WAIT_TIME 10

long int rndom() {
    srand(getpid());
    long int result = rand() % WAIT_TIME;
    return result;
}

int main() {
    setbuf(stdout, NULL); //Disable stdout buffer

    int childs = NUMBER_OF_CHILDS;
    printf("Spawning %d process\n" , childs);
    
    for (int i = 0; i < childs; i++) {
        int pid = fork();

        if (pid == 0) {
            printf("Started process with id: %d\n" , getpid());
            unsigned int s = rndom();
            sleep(s);
            printf("Exited %d after -> %d\n",getpid(),s);
            exit(s);
        }

    }

    for (int i = 0; i < childs; i++) {
        unsigned int a;
        int pid = wait(&a);
        printf("Kiled %d after %d (Exit status: %d)\n",pid,WEXITSTATUS(a),a);
    }

    return 0;
}



Spawning 3 process
Started process with id: 76
Started process with id: 78
Started process with id: 77
Exited 77 after -> 0
Kiled 77 after 0 (Exit status: 0)
Exited 76 after -> 3
Kiled 76 after 3 (Exit status: 768)
Exited 78 after -> 4
Kiled 78 after 4 (Exit status: 1024)

In [2]:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

#define NUMBER_OF_CHILDS 10
#define WAIT_TIME 3

long int rndom() {
    srand(getpid());
    long int result = rand() % WAIT_TIME;
    return result;
}

int main() {
    setbuf(stdout, NULL); //Disable stdout buffer

    int childs = NUMBER_OF_CHILDS;
    printf("Spawning %d process\n" , childs);
    
    for (int i = 0; i < childs; i++) {
        int pid = fork();

        if (pid == 0) {
            printf("Started process with id: %d\n" , getpid());
            unsigned int s = rndom();
            sleep(s);
            printf("Exited %d after -> %d\n",getpid(),s);
            exit(s);
        }

    }

    for (int i = 0; i < childs; i++) {
        unsigned int a;
        int pid = wait(&a);
        printf("Kiled %d after %d (Exit status: %d)\n",pid,WEXITSTATUS(a),a);
    }

    return 0;
}



Spawning 10 process
Started process with id: 85
Started process with id: 86
Started process with id: 88
Exited 86 after -> 0
Started process with id: 87
Started process with id: 92
Started process with id: 89
Kiled 86 after 0 (Exit status: 0)
Started process with id: 90
Started process with id: 93
Started process with id: 94
Started process with id: 91
Exited 90 after -> 0
Exited 87 after -> 0
Kiled 90 after 0 (Exit status: 0)
Exited 89 after -> 0
Kiled 87 after 0 (Exit status: 0)
Kiled 89 after 0 (Exit status: 0)
Exited 88 after -> 1
Kiled 88 after 1 (Exit status: 256)
Exited 94 after -> 1
Exited 91 after -> 1
Kiled 94 after 1 (Exit status: 256)
Kiled 91 after 1 (Exit status: 256)
Exited 85 after -> 2
Kiled 85 after 2 (Exit status: 512)
Exited 92 after -> 2
Exited 93 after -> 2
Kiled 92 after 2 (Exit status: 512)
Kiled 93 after 2 (Exit status: 512)

In [3]:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

#define NUMBER_OF_CHILDS 3
#define WAIT_TIME 10

long int rndom() {
    srand(getpid());
    long int result = rand() % WAIT_TIME;
    return result;
}

int main() {
    setbuf(stdout, NULL); //Disable stdout buffer

    int childs = NUMBER_OF_CHILDS;
    printf("Spawning %d process\n" , childs);
    
    for (int i = 0; i < childs; i++) {
        int pid = fork();

        if (pid == 0) {
            printf("Started process with id: %d\n" , getpid());
            unsigned int s = rndom();
            sleep(s);
            printf("Exited %d after -> %d\n",getpid(),s);
            exit(s);
        }

    }

    for (int i = 0; i < childs; i++) {
        unsigned int a;
        int pid = wait(&a);
        printf("Kiled %d after %d (Exit status: %d)\n",pid,WEXITSTATUS(a),a);
    }

    return 0;
}



Spawning 3 process
Started process with id: 101
Started process with id: 102
Started process with id: 103
Exited 101 after -> 6
Exited 102 after -> 6
Kiled 101 after 6 (Exit status: 1536)
Kiled 102 after 6 (Exit status: 1536)
Exited 103 after -> 9
Kiled 103 after 9 (Exit status: 2304)

In [4]:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

#define NUMBER_OF_CHILDS 10
#define WAIT_TIME 10

long int rndom() {
    srand(getpid());
    long int result = rand() % WAIT_TIME;
    return result;
}

int main() {
    setbuf(stdout, NULL); //Disable stdout buffer

    int childs = NUMBER_OF_CHILDS;
    printf("Spawning %d process\n" , childs);
    
    for (int i = 0; i < childs; i++) {
        int pid = fork();

        if (pid == 0) {
            printf("Started process with id: %d\n" , getpid());
            unsigned int s = rndom();
            sleep(s);
            printf("Exited %d after -> %d\n",getpid(),s);
            exit(s);
        }

    }

    for (int i = 0; i < childs; i++) {
        unsigned int a;
        int pid = wait(&a);
        printf("Kiled %d after %d (Exit status: %d)\n",pid,WEXITSTATUS(a),a);
    }

    return 0;
}



Spawning 10 process
Started process with id: 110
Started process with id: 111
Started process with id: 113
Started process with id: 114
Started process with id: 112
Started process with id: 117
Started process with id: 119
Started process with id: 115
Started process with id: 118
Exited 117 after -> 0
Started process with id: 116
Kiled 117 after 0 (Exit status: 0)
Exited 111 after -> 1
Kiled 111 after 1 (Exit status: 256)
Exited 114 after -> 3
Exited 112 after -> 3
Kiled 112 after 3 (Exit status: 768)
Kiled 114 after 3 (Exit status: 768)
Exited 118 after -> 4
Kiled 118 after 4 (Exit status: 1024)
Exited 113 after -> 5
Kiled 113 after 5 (Exit status: 1280)
Exited 110 after -> 6
Kiled 110 after 6 (Exit status: 1536)
Exited 116 after -> 7
Kiled 116 after 7 (Exit status: 1792)
Exited 119 after -> 9
Exited 115 after -> 9
Kiled 115 after 9 (Exit status: 2304)
Kiled 119 after 9 (Exit status: 2304)